#include<stdio.h>
#include<stdlib.h>
#include<string>
#include<iostream>
#include<cctype>
#include<cstdlib>
#include <fstream>
#include<algorithm>
#include <time.h>
#include<sstream>
#define _CRT_SECURE_NO_WARNINGS

using namespace std;

struct wordNode{
	string character;
	int number=0;
	wordNode* next;
	wordNode* pre;
};

wordNode* headNode=NULL;

string wordTrans(string word);
wordNode* wordCount(string word);
void release(wordNode* Node);
wordNode* transPosition(wordNode* node);
string getTime();

int main()
{
	//char c;
	cout << "there are five txt files" << endl;
	int filenum = 2;
	while (filenum < 4)
	{
		string extention;
		stringstream ss;
		string filename = "test";
		ss << filenum;
		ss >> extention;
		extention.append(".txt");
		filename.append(extention);
		cout << filename << endl;
		ifstream file(filename);
		ofstream fout;
		fout.open("list.txt", ios::app);
		string transType;
		string temp;
		//int num = 0;
		wordNode* wholeLink = NULL;
		if (!file.is_open())
		{
			cout << "file not Found" << endl;
		}
		else
		{
			while (file >> temp)
			{
				//cout << num<<endl;
				//num++;
				transType = wordTrans(temp);
				//system("pause");
				wholeLink = wordCount(transType);
				//cout << transType<<endl;
			}
		}
		wordNode* searchNode = wholeLink;
		string time;
		time = getTime();
		if (filenum==3)
		{
			while (searchNode != NULL)
			{
				//cout << searchNode->character << "   " << searchNode->number << endl;
				fout << searchNode->character << "   " << searchNode->number << "\n";
				searchNode = searchNode->next;
			}
		}
		cout << time << endl;
		filenum++;
		file.close();
		fout << flush;
		fout.close();
	}
	release(headNode);
	system("pause");
}

string getTime()
{
    time_t timep;	  
	time(&timep);
    char tmp[64];
    strftime(tmp, sizeof(tmp), "%Y-%m-%d %H:%M:%S", localtime(&timep));
    return tmp; 
}

string wordTrans(string word)
{
	int i = 0;
	while (i < word.size())
	{
		if (ispunct(word[i]))
		{
			int j;
			for (j = i; j < word.size() - 1; j++)	//if the punctuation is among the word
			{
				word[j] = word[j + 1];
			}
			word.erase(word.end() - 1);		//delete character before the last '\0'
		}
		else i++;
	}
	transform(word.begin(), word.end(), word.begin(), (int(*)(int))tolower);
	return word;
}

wordNode* wordCount(string temp)
{
	wordNode* head;
	wordNode* node;
	wordNode* nextNode;
	wordNode* preNode=NULL;
	if (headNode == NULL)
	{
		node = new wordNode;
		node->character = temp;
		node->number = 1;
		node->next = NULL;
		node->pre = NULL;
		headNode = node;
	}
	else
	{
		nextNode = headNode;
		while (nextNode != NULL)
		{
			//cout << "a121a" << endl;
			if (temp.compare(nextNode->character)==0)
			{
				nextNode->number=nextNode->number+1;
				head=transPosition(nextNode);
				break;
			}
			else
			{
				preNode = nextNode;
				nextNode = nextNode->next;
				//cout << "fuck" << endl;
			}
		}
		if (nextNode == NULL)
		{
			node = new wordNode;
			node->character = temp;
			node->number=1;
			preNode->next = node;
			node->pre = preNode;
			node->next = NULL;
		}
	}
	return headNode;
}

wordNode* transPosition(wordNode* node)
{
	wordNode* Node = node;
	wordNode* PreNode = NULL;
	int i = 0;
	while (Node != NULL)
	{
		i++;
		//cout << i << endl;
		PreNode = Node;
		Node = PreNode->pre;
		if (Node!=NULL&&Node->number <= node->number)
			continue;
		else
		{	
			if (Node == NULL)
				break;
			else
			{
				if (node->next != NULL)
				{
					if (node->pre->number >= node->number)
					{
						return headNode;
					}
					else
					{
						node->next->pre = node->pre;
						node->pre->next = node->next;
						Node->next = node;
						node->pre = Node;
						PreNode->pre = node;
						node->next = PreNode;
						//return headNode;
						break;
					}
				}
				else
				{
					if (node->pre->number >= node->number)
					{
						return headNode;
					}
					else
					{
						node->pre->next = NULL;
						Node->next = node;
						node->pre = Node;
						PreNode->pre = node;
						node->next = PreNode;
						//return headNode;
						break;
					}
				}
			}
		}
	}
	if (Node == NULL&&i!=1)
	{
		if (node->next != NULL)
		{
			node->next->pre = node->pre;
			node->pre->next = node->next;
			node->pre = NULL;
			node->next = PreNode;
			PreNode->pre = node;
			headNode = node;
			return headNode;
		}
		else
		{
			node->pre->next = NULL;
			node->pre = NULL;
			node->next = PreNode;
			PreNode->pre = node;
			headNode = node;
			return headNode;
		}
	}
	else
	{
		return headNode;
	}
	return headNode;
}

void release(wordNode* node)
{
	wordNode* nextNode = NULL;
	while (node != NULL)
	{
		nextNode=node->next;
		free(node);
		node = nextNode;
	}
}